﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Web;

namespace WhsFacebook.Net.Web.Facebook
{
    public partial class ChangeTowerImage : CanvasFBMLBasePage
    {
        public ChangeTowerImage()
        {
            this.RequireLogin = true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            using (var db = new Database.WhsFacebookEntities())
            {
                // query instance id
                var defMaster = (DefaultMaster)this.Master;
                var userId = defMaster.FacebookVisit.UserId;

                var instanceId = (
                    from t in db.FacebookInstanceMappings
                    where t.FacebookUserId == userId
                    select t.InstanceId
                    ).First();

                // query tower image
                {
                    var img = db.UserWhsTowerImages.Where(o => o.InstanceId == instanceId).FirstOrDefault();

                    if (img == null)
                    {
                        this.towerImage.Src = this.ResolveUrlAbsolute("~/Images/NoTowerImage.jpg");
                    }
                    else
                    {
                        this.towerImage.Src = string.Format(
                            "{0}?id={1}",
                            this.ResolveUrlAbsolute("~/Facebook/Handlers/TowerImage.ashx"),
                            instanceId
                            );
                    }
                }
            }

        }
    }
}